import * as CryptoJS from 'crypto-js'

// 表单序列化
export const serialize = (data:any) => {
    const list:Array<any> = []
    Object.keys(data).forEach(ele => {
      list.push(`${ele}=${data[ele]}`)
    })
    return list.join('&')
  }

export function getUrlKey (name:any,url:string) {
    let c: any = (new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(url ? url : location.href) || [, ""])[1]
    return decodeURIComponent(c.replace(/\+/g, '%20')) || null
}

/**
 *加密处理
 */
 export const encryption = (params:any) => {
    let {
      data,
      type,
      param,
      key
    } = params
    const result = JSON.parse(JSON.stringify(data))
    if (type === 'Base64') {
      param.forEach((ele:any) => {
        result[ele] = btoa(result[ele])
      })
    } else {
      param.forEach((ele:any) => {
        var data = result[ele]
        key = CryptoJS.enc.Latin1.parse(key)
        var iv = key
        // 加密
        var encrypted = CryptoJS.AES.encrypt(
          data,
          key, {
            iv: iv,
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.ZeroPadding
          })
        result[ele] = encrypted.toString()
      })
    }
    return result
  }

export const getAssetsFile = (url: string) => {
    return new URL(`../assets/img/${url}`, import.meta.url).href
}

export function getSignal(signal) {
    switch (signal) {
      case null:
        return getAssetsFile('/signal/signal_error.png');
      case 0:
        return getAssetsFile('/signal/0signal.png');
      case 1:
        return getAssetsFile('/signal/1signal.png');
      case 2:
        return getAssetsFile('/signal/2signal.png');
      case 3:
        return getAssetsFile('/signal/3signal.png');
      case 4:
        return getAssetsFile('/signal/4signal.png');
      case 5:
        return getAssetsFile('/signal/5signal.png');
      case 6:
        return getAssetsFile('/signal/6signal.png');
      case 7:
        return getAssetsFile('/signal/7signal.png');
    }
  }

    // UTC转本地时间
export function utcFormat(time, format) {
    var t = new Date(time * 1000);
    var tf = function (i) {
        return (i < 10 ? '0' : '') + i
    };
    return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function (a) {
        switch (a) {
          case 'yyyy':
              return tf(t.getFullYear());
              break;
          case 'MM':
              return tf(t.getMonth() + 1);
              break;
          case 'mm':
              return tf(t.getMinutes());
              break;
          case 'dd':
              return tf(t.getDate());
              break;
          case 'HH':
              return tf(t.getHours());
              break;
          case 'ss':
              return tf(t.getSeconds());
              break;
      }
    })
  }

/**
 * 毫秒数格式化
 */
 export function formatDuring(value:number): string{
    let hours:string|number = parseInt(String(value % (1000 * 60 * 60 * 24) / (1000 * 60 * 60)));
    let minutes:string|number = parseInt(String(value % (1000 * 60 * 60) / (1000 * 60)));
    let seconds:string|number = value % (1000 * 60) / 1000;
    if (hours < 10) {
      hours = '0' + hours
    }
    if (minutes < 10) {
      minutes = '0' + minutes
    }
    if (seconds < 10) {
      seconds = '0' + seconds
    }
    return hours + ":" + minutes + ":" + seconds;
}

/**
 * 通过ref 删除className
 * @param ref_info 数组[this.$refs.class1, this.$refs.class2]
 * @param class_name 需要移除的名称
 */
export function refRemoveClass(ref_info:Array<any>, class_name:string): void{
    ref_info.forEach((element) => {
        // 区分是否为组件内，使用需要用 $el
        let class_name_arr = element.$el
            ? element.$el.className.split(" ")
            : element.className.split(" ");
        let index = class_name_arr.findIndex((item) => {
            return item == class_name;
        });
        if (index !== -1) class_name_arr.splice(index, 1);
        // 区分是否为组件内，使用需要用 $el
        if (element.$el) {
            element.$el.className = class_name_arr.join(" ");
        } else {
            element.className = class_name_arr.join(" ");
        }
    });
}